package com.yekaile.carsystem.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yekaile.carsystem.controller.vo.RoleAuthorityVO;
import com.yekaile.carsystem.controller.vo.RoleVO;
import com.yekaile.carsystem.entity.Role;
import com.yekaile.carsystem.service.RoleService;
import com.yekaile.carsystem.util.Result;
import lombok.AllArgsConstructor;
import org.apache.http.HttpStatus;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yekaile
 */
@RestController
@RequestMapping("role")
@AllArgsConstructor(onConstructor_=@Autowired)
public class RoleController {


    private final RoleService roleService;



    @PostMapping("addRole")
    @RequiresPermissions("role:add")
    public Result<String> addRole(@RequestBody RoleAuthorityVO roleAuthorityVO) {
        roleService.addRole(roleAuthorityVO);
        return new Result<>(HttpStatus.SC_CREATED, "success", "success");
    }

    @GetMapping("getOnlyAllRoles")
    @RequiresPermissions("role:select")
    public Result<List<Role>>listResultOnlyAllRoles(){
        List<Role> roles = roleService.getOnlyAllRoles();
        return new Result<>(HttpStatus.SC_OK,"success",roles);
    }



    @DeleteMapping("deleteRole/{id}")
    @RequiresPermissions("role:delete")
    public Result<String> deleteRole(@PathVariable("id") Integer roleId) {
        roleService.deleteRole(roleId);
        return new Result<>(HttpStatus.SC_NO_CONTENT, "success", "success");
    }

    @GetMapping("getAllRoles")
    @RequiresPermissions("role:select")
    public Result<PageInfo<RoleVO>> listAllRoles(@RequestParam(defaultValue = "1", required = false) Integer pageNum,
                                                @RequestParam(defaultValue = "4", required = false) Integer pageSize) {
        return new Result<>(HttpStatus.SC_OK, "success", roleService.getAllRoles(pageNum, pageSize));
    }

    @GetMapping("getRole")
    @RequiresPermissions("role:select")
    public Result<RoleVO> getRole(Integer roleId) {
        RoleVO roleVO = roleService.getRole(roleId);
        return new Result<>(HttpStatus.SC_OK, "success", roleVO);
    }

    @PutMapping("updateRole")
    @RequiresPermissions("role:update")
    public Result<String> updateRole(@RequestBody RoleAuthorityVO roleAuthorityVO) {
        roleService.updateRole(roleAuthorityVO);
        return new Result<>(HttpStatus.SC_CREATED, "success", "success");

    }


}
